package com.af.community.smart.business

import android.os.Bundle
import android.view.View
import com.af.community.smart.base.*
import com.af.community.smart.model.CommonData
import com.af.community.smart.utils.bindLifeCycle
import com.af.community.smart.utils.observe
import com.af.community.smart.utils.subscribeByFinally
import com.af.community.smart.utils.toFormatString
import com.af.community.smart.viewmodel.DailyViewModel
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.include
import org.jetbrains.anko.startActivity
import org.koin.androidx.viewmodel.ext.android.getViewModel

class DailyActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        include<View>(R.layout.layout_list)
        initTitle(title = "民情日志", name = "新增")

        swipe_refresh.isRefreshing = true
        getData(pageNum)
    }

    override fun getViewModel() = getViewModel<DailyViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        empty_hint.text = "暂无相关民情日志信息！"
        swipe_refresh.refresh { getData(1) }
        recycle_list.loadLinear(baseContext, swipe_refresh) {
            if (!isLoadingMore) {
                isLoadingMore = true
                getData(pageNum)
            }
        }

        mAdapter = SlimAdapter.create()
            .register<CommonData>(R.layout.item_daily_list) { data, injector ->
                injector.text(R.id.item_daily_time, data.createDate?.toFormatString() ?: "")
                    .text(R.id.item_daily_content, data.content)
            }
            .attachTo(recycle_list)

        tvRight?.oneClick { startActivity<DailyAddActivity>() }

        observe("daily_add") { updateList() }
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().getDailyList(index, getString("userId"))
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mList.apply {
                        if (index == 1) {
                            clear()
                            pageNum = index
                        }
                        addItems(it)
                        if (count(it) > 0) pageNum++
                    }

                    mAdapter.updateData(mList)
                },
                onFinally = {
                    swipe_refresh.isRefreshing = false
                    isLoadingMore = false
                    empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                }
            )
    }

    private fun updateList() {
        swipe_refresh.isRefreshing = true

        empty_view.gone()
        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        pageNum = 1
        getData(pageNum)
    }

}
